﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.ComponentModel;
using System.Drawing.Design;
using NetJsWire.Lite.Converters;
using NetJsWire.Lite.Editors;
using Newtonsoft.Json;

namespace NetJsWire.Lite.Widgets.UI.Grid
{
    [Serializable]
    [JsonObject( MemberSerialization = MemberSerialization.OptOut )]
    public class Column
    {
        private bool _editable = true;
        private bool _groupable = true;
        private string _header;
        private bool _hideable = true;

        [NonSerialized]
        private ColumnModel _modelOwner;

        private bool _resizable = true;
        private int _width = 100;

        internal Column()
            : this( null )
        {
        }

        public Column( ColumnModel owner )
        {
            //if ( owner == null )
            //{
            //    throw new NullReferenceException( "The owner cannot be null" );
            //}
            _modelOwner = owner;
        }

        internal ColumnModel Owner { get { return _modelOwner; } set { _modelOwner = value; } }

        [DefaultValue( ( string ) null )]
        [JsonProperty( "align" )]
        public string Align { get; set; }

        [DefaultValue( ( string ) null )]
        [JsonProperty( "css" )]
        public string Css { get; set; }

        [DefaultValue( ( string ) null )]
        [JsonProperty( "dataIndex" )]
        public string DataIndex { get; set; }

        [DefaultValue( true )]
        [JsonProperty( "editable" )]
        public bool Editable
        {
            get { return _editable; }
            set { _editable = value; }
        }

        [DefaultValue( ( string ) null )]
        [JsonConverter( typeof( NotQuotedJsonStringConverter ) )]
        [JsonProperty( "editor" )]
        [Editor( typeof( ScriptEditor ), typeof( UITypeEditor ) )]
        public string Editor { get; set; }

        [DefaultValue( ( string ) null )]
        [JsonProperty( "emptyGroupText" )]
        public string EmptyGroupText { get; set; }

        [DefaultValue( false )]
        [JsonProperty( "fixed" )]
        public bool Fixed { get; set; }

        [DefaultValue( ( string ) null )]
        [JsonProperty( "groupName" )]
        public string GroupName { get; set; }

        [DefaultValue( ( string ) null )]
        [JsonProperty( "groupRenderer" )]
        public string GroupRenderer { get; set; }

        [DefaultValue( true )]
        [JsonProperty( "groupable" )]
        public bool Groupable
        {
            get { return _groupable; }
            set { _groupable = value; }
        }

        [DefaultValue( ( string ) null )]
        [JsonProperty( "header" )]
        public string Header
        {
            get { return _header; }
            set
            {
                _header = value;
                if ( _modelOwner != null )
                    _modelOwner.NotifyParentToRedraw();
            }
        }

        [DefaultValue( false )]
        [JsonProperty( "hidden" )]
        public bool Hidden { get; set; }

        [DefaultValue( true )]
        [JsonProperty( "hideable" )]
        public bool Hideable
        {
            get { return _hideable; }
            set { _hideable = value; }
        }

        [DefaultValue( ( string ) null )]
        [JsonProperty( "id" )]
        public string Id { get; set; }

        [DefaultValue( false )]
        [JsonProperty( "menuDisabled" )]
        public bool MenuDisabled { get; set; }

        [DefaultValue( ( string ) null )]
        [JsonIgnore]
        public string Name { get; set; }

        [DefaultValue( ( string ) null )]
        [JsonConverter( typeof( NotQuotedJsonStringConverter ) )]
        [JsonProperty( "renderer" )]
        [Editor( typeof( ScriptEditor ), typeof( UITypeEditor ) )]
        public string Renderer { get; set; }

        [DefaultValue( true )]
        [JsonProperty( "resizable" )]
        public bool Resizable
        {
            get { return _resizable; }
            set { _resizable = value; }
        }

        [DefaultValue( ( string ) null )]
        [JsonConverter( typeof( NotQuotedJsonStringConverter ) )]
        [JsonProperty( "scope" )]
        public string Scope { get; set; }

        [DefaultValue( false ), JsonProperty( "sortable" )]
        public bool Sortable { get; set; }

        [DefaultValue( ( string ) null )]
        [JsonProperty( "summaryType" )]
        public string SummaryType { get; set; }

        [DefaultValue( ( string ) null )]
        [JsonConverter( typeof( NotQuotedJsonStringConverter ) )]
        [Editor( typeof( ScriptEditor ), typeof( UITypeEditor ) )]
        [JsonProperty( "summaryRenderer" )]
        public string SummaryRenderer { get; set; }

        [DefaultValue( ( string ) null )]
        [JsonProperty( "tooltip" )]
        public string Tooltip { get; set; }

        [DefaultValue( 100 )]
        [JsonProperty( "width" )]
        public int Width
        {
            get { return _width; }
            set
            {
                _width = value;
                if ( _modelOwner != null )
                    _modelOwner.NotifyParentToRedraw();
            }
        }

        [DefaultValue( ( string ) null )]
        [JsonProperty( "xtype", NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Include )]
        [TypeConverter( typeof( ColumnXtypeTypeConverter ) )]
        public string Xtype { get; set; }
    }
}
